﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MCSFramework.Model.CM;
using MCSFramework.BLL.CM;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //btnChg.Attributes["onClick"] = "return confirm('确定要更新该记录？', '确定', '取消')";

            GridView1.AllowPaging = true;
            GridView1.PageSize = 15;


            this.Bind();
        }

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        int newPageIndex = 0;
        if (e.NewPageIndex < 0)
        {
            //点击了Go按钮
            TextBox txtNewPageIndex = null;

            //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
            GridViewRow pagerRow = GridView1.BottomPagerRow;

            if (pagerRow != null)
            {
                //得到text控件
                txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
            }
            if (txtNewPageIndex != null)
            {
                //得到索引
                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
            }
        }
        else
        {
            //点击了其他的按钮
            newPageIndex = e.NewPageIndex;
        }
        //防止新索引溢出
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex = newPageIndex >= GridView1.PageCount ? GridView1.PageCount - 1 : newPageIndex;

        //得到新的值
        GridView1.PageIndex = newPageIndex;

        //重新绑定
        this.Bind();

    }

    void Bind()
    {
        IList<CM_Client> ClientList = CM_ClientBLL.GetModelList("1=1");
        GridView1.DataSource = ClientList;
        GridView1.DataBind();
    }

    protected void LinkButton_Click(object sender, EventArgs e)
    {

        LinkButton btn = sender as LinkButton;
        GridViewRow row = btn.Parent.Parent as GridViewRow;
        string a = row.Cells[1].Text;
        Response.Write(a);
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Choose")//点击了选择按钮
        {
            LinkButton linkBtn = e.CommandSource as LinkButton;
            GridViewRow row = linkBtn.Parent.Parent as GridViewRow;
            string id = row.Cells[1].Text;
            IList<CM_Client> clientList = CM_ClientBLL.GetModelList("id=" + id);

            CM_Client client = clientList[0];
            ViewState["CM_Client"] = client;

            txtFullName.Text = client.FullName;
            txtAddress.Text = client.Address;
            txtTeleNum.Text = client.TeleNum;
            txtPostCode.Text = client.PostCode;
        }
    }
    protected void btnChg_Click(object sender, EventArgs e)
    {
        if (ViewState["CM_Client"] != null)
        {

            CM_Client client = ViewState["CM_Client"] as CM_Client;
            client.FullName = txtFullName.Text;
            client.Address = txtAddress.Text;
            client.TeleNum = txtTeleNum.Text;
            client.PostCode = txtPostCode.Text;

            CM_ClientBLL clientBLL = new CM_ClientBLL();
            clientBLL.Model = client;
            clientBLL.Update();
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        CM_ClientBLL clientBLL = new CM_ClientBLL();
        CM_Client client = new CM_Client();
        client.Code = "000001";
        client.FullName = txtFullName.Text;
        client.ShortName = "公司仓库";
        client.OrganizeCity = 1;
        client.OfficalCity = 1;
        client.Address = "2345";
        client.TeleNum = txtTeleNum.Text;
        client.Fax = "chingy";
        client.Email = "chngy";
        client.Url = "";
        client.PostCode = txtPostCode.Text;
        client.ChiefLinkMan = 3;
        client.ActiveFlag = 1;
        client.OpenTime = DateTime.Parse("2009-01-01 00:00:00.000");
        client.CloseTime = DateTime.Parse("2014-07-08 16:56:59.987");
        client.ClientType = 1;
        client.Supplier = 8;
        client.ClientManager = 0;
        client.ApproveFlag = 1;
        client.InsertStaff = 1;
        client.ExtPropertys = "||||1|||||||||||||2||||||||||||4||||4|50|40||||2|";
        clientBLL.Model = client;
        clientBLL.Add();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        CM_ClientBLL clientBLL = new CM_ClientBLL();
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)GridView1.Rows[i].FindControl("chkClient_ID");
            if (ck.Checked == true)
            {
                int a = int.Parse(GridView1.DataKeys[i].Value.ToString());
                int b = clientBLL.Delete(a);
            }
        }
        this.Bind();

    }
}
